package org.example.ssh

import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.JSONB
import net.schmizz.sshj.SSHClient
import org.example.ssh.base.SSH
import org.example.ssh.base.SshCtx
import org.example.ssh.base.executeCmdPrint
import org.example.ssh.base.uploadDirectoryRecursive
import test.t1.TurnCredentialGenerator

suspend fun main() {
    val list = SSH.getGroup("v2board")
    SSH.execF(list) {
//        DeployX2Ray().pullImages(it)
        DeployX2Ray().deploy(it)
//        DeployX2Ray().deployXray(it)
    }
}

class DeployX2Ray {
    suspend fun deploy(sshCtx: SshCtx) {
        println("执行")
//        val sftp = sshCtx.cli.newSFTPClient()
//        uploadDirectoryRecursive(sftp, "./relay配置/v2board", "/home/x2ray")
//        executeCmdPrint(sshCtx.cli, "cd /home/x2ray/v2board && docker-compose -f docker-compose.yml down")
//        executeCmdPrint(sshCtx.cli, "cd /home/x2ray/v2board && docker-compose -f docker-compose.yml up -d")
//        executeCmdPrint(sshCtx.cli, "docker ps")
//        executeCmdPrint(sshCtx.cli, "cd /home/x2ray/v2board && docker-compose logs v2board-app")

//        executeCmdPrint(
//            sshCtx.cli,
//            "docker exec v2board-app sh -c 'cd /usr/share/nginx/html/v2board && sh init.sh && chmod -R 777 /usr/share/nginx/html/v2board'"
//        )
//        executeCmdPrint(sshCtx.cli, "docker exec v2board-app sh -c 'cd /usr/share/nginx/html/v2board && supervisorctl restart v2board'")
        val s = "cd /home/x2ray/v2board-master"
//        executeCmdPrint(sshCtx.cli, "$s && chmod +x ./init.sh")
//        executeCmdPrint(sshCtx.cli, "$s && ./init.sh")
//        executeCmdPrint(sshCtx.cli, "kill -9 2958938")
        executeCmdPrint(sshCtx.cli, "lsof -i :8080")
//        executeCmdPrint(sshCtx.cli, "$s && php artisan serve --host=38.147.171.207 --port=8080")
//# 启动并输出日志到文件
//        php artisan serve --host=38.147.171.207 --port=8080
//nohup php artisan serve --host=38.147.171.207 --port=8080 > /home/x2ray/v2board.log 2>&1 &
//
//# 查看是否运行
//ps aux | grep "php artisan serve"                             pkill -f "artisan serve"
    }

    suspend fun deployXray(sshCtx: SshCtx) {
//        val sftp = sshCtx.cli.newSFTPClient()
//        uploadDirectoryRecursive(sftp, "./relay配置/xray", "/home/x2ray")
//        executeCmdPrint(sshCtx.cli, "docker ps")
//        executeCmdPrint(sshCtx.cli, "cd /home/x2ray/xray && docker-compose -f docker-compose.yml down")
//        executeCmdPrint(sshCtx.cli, "cd /home/x2ray/xray && docker-compose -f docker-compose.yml up -d")
//        executeCmdPrint(sshCtx.cli, "cd /home/x2ray/xray && docker-compose logs v2ray")


    }

    suspend fun pullImages(sshCtx: SshCtx) {
        executeCmdPrint(sshCtx.cli, "docker pull v2fly/v2fly-core:v5.30.0")
        executeCmdPrint(sshCtx.cli, "docker pull gz1903/v2board:1.7.3")
        executeCmdPrint(sshCtx.cli, "docker images")
    }

}










